package com.gx.smart.building;

import com.casic.gx.grpc.common.ComQuery;
import com.gx.smart.lib.http.api.asynctask.CallBack;
import com.gx.smart.lib.http.api.asynctask.GrpcAsyncTask;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.gx.smart.lib_http.BuildConfig;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmApiGrpc;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmDealReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmDealResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmDetailQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmDetailQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmQueryReq;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.AlarmQueryResp;
import com.huishi.gx.ib.grpc.lib.ib.admin.maintenance.messagealarm.v1.MessageAlarmDto;
import com.orhanobut.logger.Logger;

import java.util.concurrent.TimeUnit;

import io.grpc.ManagedChannel;

/**
 * @author: xiaosy
 * 时间: 2022/4/7
 * @Description: java类作用描述
 * @Version: 1.0
 **/
public class AlarmApiService {
    private String host = BuildConfig.JOYLIFE_GRPC_UAA_SERIVER_URL;
    private String port = "80";

    private static class SingleTonHolder {
        private static final AlarmApiService INSTANCE = new AlarmApiService();
    }

    public static AlarmApiService getInstance() {
        return SingleTonHolder.INSTANCE;
    }

    private AlarmApiService() {

    }

    /**
     * 获取AuthStub  设置超时时间  8秒
     *
     * @param channel
     * @return
     */
    private AlarmApiGrpc.AlarmApiBlockingStub getStub(ManagedChannel channel) {
        return AlarmApiGrpc.newBlockingStub(channel)
                .withDeadlineAfter(ApiConfig.TIMEOUT_NETWORK, TimeUnit.SECONDS);

    }

    /**
     * 查询告警列表
     *告警解决状态： 1-未处理，2-已处理
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AlarmQueryResp> query(
            ComQuery comQuery,
            int status,
            String userId,
            CallBack<AlarmQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AlarmQueryResp>(callBack) {
            @Override
            protected AlarmQueryResp doRequestData(ManagedChannel channel) {
                AlarmQueryReq message = AlarmQueryReq.newBuilder()
                        .setComQuery(comQuery)
                        .setAlarmResolveState(status)
                        .setProcessUserId(userId)
                        .build();
                Logger.d(message);
                AlarmQueryResp response = null;
                try {
                    response = getStub(channel).query(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "query");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 查看告警详情
     *
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AlarmDetailQueryResp> detail(
            String id,
            CallBack<AlarmDetailQueryResp> callBack) {
        return new GrpcAsyncTask<String, Void, AlarmDetailQueryResp>(callBack) {
            @Override
            protected AlarmDetailQueryResp doRequestData(ManagedChannel channel) {
                AlarmDetailQueryReq message = AlarmDetailQueryReq.newBuilder()
                        .setId(id)
                        .setTenantNo(1)
                        .build();
                Logger.d(message);
                AlarmDetailQueryResp response = null;
                try {
                    response = getStub(channel).detail(message);
                    Logger.d(response);
                } catch (Exception e) {
                    Logger.e(e, "detail");
                }
                return response;
            }
        }.setHost(host).setPort(port).doExecute();
    }

    /**
     * 处理告警
     * @param callBack
     * @return
     */
    public GrpcAsyncTask<String, Void, AlarmDealResp> deal(MessageAlarmDto messageAlarmDto, CallBack<AlarmDealResp> callBack) {
            return new GrpcAsyncTask<String, Void, AlarmDealResp>(callBack) {
                @Override
                protected AlarmDealResp doRequestData(ManagedChannel channel) {
                    AlarmDealReq message = AlarmDealReq.newBuilder()
                            .setContent(messageAlarmDto)
                            .build();
                    Logger.d(message);
                    AlarmDealResp response = null;
                    try {
                        response = getStub(channel).deal(message);
                        Logger.d(response);
                    } catch (Exception e) {
                        Logger.e(e, "deal");
                    }
                    return response;
                }
            }.setHost(host).setPort(port).doExecute();
        }
}
